//
//  WithdrawalViewController.m
//  WBBaseProject
//
//  Created by 吴斌 on 2018/4/10.
//  Copyright © 2018年 吴斌. All rights reserved.
//

#import "WithdrawalViewController.h"
#import "WithdrawTFView.h"
#import "WithdrawRecordViewController.h"
#import "ValuePickerView.h"
#import "ZJPayPopupView.h"

@interface WithdrawalViewController ()<UITextFieldDelegate,ZJPayPopupViewDelegate>
{
    NSArray *bankListArr;
    NSMutableArray *bankNameList;
}
@property(nonatomic,strong)ValuePickerView *bankList;
@property (nonatomic, strong) WithdrawTFView *withdrawTF;

@property (nonatomic, strong) ZJPayPopupView *payPopupView;
@end

@implementation WithdrawalViewController

#pragma mark - 生命周期 Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"钱包提现";
    self.view.backgroundColor = WB_RGBColor(251, 251, 251);
    [self setupUI];
    [self addRightButton];
    
    [self getbank];
}
- (void)getbank
{
    //我的银行卡列表
    [XYHttpRequest dataRequestWithMyBankCardListType:0 success:^(id responseObj) {
        
        if (Success) {
            bankListArr = responseObj[@"data"][@"result"];
            bankNameList = [NSMutableArray array];
            
            for (NSDictionary* dic in bankListArr) {
                
                [bankNameList addObject:dic[@"desCardName"]];
            }
            
            self.bankList.dataSource = bankNameList;
        }
    } failure:^(NSError *error) {
        
    }];
    //默认银行卡
    [XYHttpRequest dataRequestWithDefaultBankCardsuccess:^(id responseObj) {
        
        if (Success) {
            
            NSArray *arr = responseObj[@"data"][@"result"];
            
            if (!arr.count) {
                
                [NSObject wb_showAlertWithTitle:@"暂未绑卡" duration:1];
                return;
            }
            NSDictionary* dic = arr[0];
//            self.comfirmPayView.nameLb.text = dic[@"cardCustName"];
//            [self.comfirmPayView.bankInfoLb setTitle:dic[@"desCardName"] forState:UIControlStateNormal];
//            [self.comfirmPayView show];
//            self.comfirmPayView.bankData = bankList;
//
//            name = bankListArr[row][@"desCardName"];
            [self.withdrawTF.bankCardNumber setTitle:dic[@"desCardName"] forState:UIControlStateNormal];
        }
        
    } failure:^(NSError *error) {
        
    }];
}
- (void)setupUI
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavigationBarHeight)];
    scrollView.contentSize = CGSizeMake(kScreenWidth, kScreenHeight - kNavigationBarHeight + 1);
    scrollView.bounces = YES;
    scrollView.backgroundColor = WB_RGBColor(240, 240, 240);
    [self.view addSubview:scrollView];
    [scrollView addSubview:self.withdrawTF];
}

- (void)addRightButton
{
    UIButton *btn = [UIButton ba_buttonWithFrame:CGRectMake(0, 0, 80, 64) title:@"记录" titleColor:[UIColor whiteColor] titleFont:[UIFont systemFontOfSize:13]];
    [btn ba_button_setButtonLayoutType:BAKit_ButtonLayoutTypeRightImageLeft padding:2.0f];
    btn.titleLabel.textAlignment = NSTextAlignmentRight;
    btn.frame = CGRectMake(0, 0, 80, 64);
    //把视图的边角变为圆形, cornerRadius圆角半径
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    //弹簧控件, 修复边距
    UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    spaceItem.width = -5;
    self.navigationItem.rightBarButtonItems = @[spaceItem,backItem];
    [btn addTarget:self action:@selector(clickDetail:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)clickDetail:(UIButton *)sender
{
    WithdrawRecordViewController *vc = [[WithdrawRecordViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载  Lazy Load
- (WithdrawTFView *)withdrawTF
{
    if (_withdrawTF == nil) {
        _withdrawTF = [[NSBundle mainBundle] loadNibNamed:@"WithdrawTFView" owner:self options:nil].firstObject;
        _withdrawTF.frame = CGRectMake(15, 25, kScreenWidth - 30, 290);
        
        _withdrawTF.allWithdraw = self.allMoney;
        
        WB_WEAKSELF;
        _withdrawTF.applyClick = ^{
            //提现
            [weakSelf toWithdraw];
        };

        _withdrawTF.chooseBank = ^{
            //选择银行卡
            [weakSelf.view endEditing:YES];
            [weakSelf.view addSubview:weakSelf.bankList];
            
            [weakSelf.bankList show];
        };
    }
    return _withdrawTF;
}
- (void)toWithdraw
{
    if (WB_IsNilString(_withdrawTF.moneyTF.text)||[_withdrawTF.moneyTF.text isEqualToString:@"0"]) {
        [NSObject wb_showAlertWithTitle:@"请输入金额" duration:1];
        return;
    }
    [_withdrawTF.moneyTF resignFirstResponder];
    
    self.payPopupView = [[ZJPayPopupView alloc] init];
    self.payPopupView.delegate = self;
    [self.payPopupView showPayPopView];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.text.integerValue>self.allMoney.integerValue) {
        
        textField.text = [NSString stringWithFormat:@"%@",self.allMoney];
        
    }
}

- (ValuePickerView *)bankList
{
    if (!_bankList) {
        
        _bankList = [[ValuePickerView alloc]init];
        
        WB_WEAKSELF;
        __block NSString *name;
        _bankList.valueDidSelect = ^(NSInteger row) {
            
            NSLog(@"%ld",(long)row);
            
            //bankName = bankListArr[row][@"NAME"];
            name = bankListArr[row][@"desCardName"];
            [weakSelf.withdrawTF.bankCardNumber setTitle:name forState:UIControlStateNormal];
            //[weakSelf.withdrawTF.bankCardNumber setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        };
        
    }
    return _bankList;
}
#pragma mark - ZJPayPopupViewDelegate

- (void)didClickForgetPasswordButton
{
    NSLog(@"点击了忘记密码");
}

- (void)didPasswordInputFinished:(NSString *)password
{
    WB_WeakSelf(self)
    [XYHttpRequest dataRequestWithCheckTraderOldPwd:password success:^(id responseObj) {
        if (Success) {
            //隐藏键盘
            [weakself.payPopupView hidePayPopView];
            [XYHttpRequest dataRequestWithDrawMoney:_withdrawTF.moneyTF.text success:^(id responseObj) {
                if (Success) {

                    [NSOrderedSet wb_showAlertWithTitle:@"提现成功！"];
                    
                    WB_DISPATCH_MAIN_Delay_THREAD(1, ^{
                       
                        [self.navigationController popViewControllerAnimated:YES];
                    });
                    
                }else{
                    [NSObject wb_showAlertWithTitle:responseObj[@"info"] duration:1];
                }
            } failure:^(NSError *error) {
            }];
        }else{
            
            [NSObject wb_showAlertWithTitle:responseObj[@"info"]];
        }
    } failure:^(NSError *error) {
        
    }];
}

@end
